package com.fujifilm.project.plan.security;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.fujifilm.project.plan.entity.User;
import com.fujifilm.project.plan.entity.table.UserTableDef;
import com.fujifilm.project.plan.mapper.UserMapper;
import com.mybatisflex.core.query.QueryWrapper;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * 
 * @date 2025年6月09日
 * @time 23:12:50
 * @description
 * 
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class JwtUserDetailsService implements UserDetailsService {

    private final UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper query = QueryWrapper.create()
                .where(UserTableDef.USER.USERNAME.eq(username))
                .or(UserTableDef.USER.EMAIL.eq(username))
                .limit(1);
        User user = userMapper.selectOneByQuery(query);
        if (user == null) {
            log.warn("User not found with username: {}", username);
            return null; // 或者抛出异常
        }
        return new UserImplDetails(user);
    }

}
